<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Goods extends NotORM {

    //设置表名
    protected function getTableName($id) {
        return 'goods';
    }
    // 按条件获取商品
    public function getGoodsByCon( $condition, $str, $curpage, $like ) {
        if( $like ){
            return $this->getORM()
                ->select('goods_id,cat_id,goods_name,original_img,goods_remark,market_price,shop_price,virtual_sales_sum,prom_type,prom_id,prom_price')
                ->where( $condition )
                ->where('goods_name LIKE ?',"%$like%")
                ->order( $str )
                ->limit( $curpage, 10 )
                ->fetchAll();
        }
        return $this->getORM()
            ->select('goods_id,cat_id,goods_name,original_img,goods_remark,market_price,shop_price,virtual_sales_sum,prom_type,prom_id,prom_price')
            ->where( $condition )
            ->order( $str )
            ->limit( $curpage, 10 )
            ->fetchAll();
    }

    //获取总数量，分页使用
    public function getGoodsByConNum( $condition, $str, $like ) {
        if( $like ){
            return $this->getORM()
                ->where( $condition )
                ->where('goods_name LIKE ?',"%$like%")
                ->order( $str )
                ->count();
        }
        return $this->getORM()
            ->where( $condition )
            ->order( $str )
            ->count();
    }

    // 商品详情
    public function getGoodsDetail( $goods_id ) {
        return $this->getORM()
            ->select('goods_id,cat_id,admin_id,goods_name,goods_remark,market_price,shop_price,virtual_sales_sum,comment_count,goods_content_img,prom_type,prom_id,prom_price,is_on_sale')
            ->where( array('goods_id'=>$goods_id) )
            ->fetchOne();
    }

    // 商品图片
    public function getGoodsImages( $goods_id ) {
        $orm = \PhalApi\DI()->notorm->goods_images;
        return $orm
            ->select('image_url')
            ->where( array('goods_id'=>$goods_id) )
            ->fetchAll();
    }

    //获取评论总数量，分页使用
    public function getCommentsByConNum( $goods_id ) {
        $orm = \PhalApi\DI()->notorm->comment;
        return $orm
            ->where(  array('goods_id'=>$goods_id)  )
            ->count();
    }

    // 商品评论
    public function getGoodsComment( $goods_id, $limit = 0 ) {
        $orm = \PhalApi\DI()->notorm->comment;
        return $orm
            ->select('user_id,username,head_pic,content,img,add_time')
            ->where( array('goods_id'=>$goods_id) )
            ->order( 'add_time DESC' )
            ->limit( $limit, 10 )
            ->fetchAll();
    }

    // 商品规格（三级）
    public function getGoodsSpec( $goods_id ) {
        $orm = \PhalApi\DI()->notorm->spec_goods_price;
        return $orm
            ->select('*')
            ->where( array('goods_id'=>$goods_id) )
            ->order('price DESC')
            ->fetchAll();
    }

    // 商品规格item（二级）
    public function getGoodsSpecItem( $item_id ) {
        $orm = \PhalApi\DI()->notorm->spec_item;
        return $orm
            ->select('*')
            ->where( array('id'=>$item_id) )
            ->fetchOne();
    }

    // 添加购物车
    public function addCart( $data ) {
        $orm = \PhalApi\DI()->notorm->cart;
        $orm->insert( $data );
        return $orm->insert_id();
    }

    // 购物车列表
    public function cartList( $user_id ) {
        $orm = \PhalApi\DI()->notorm->cart;
        return $orm
            ->select('id,user_id,goods_id,goods_name,spec_key,prom_type,market_price,goods_price,goods_num')
            ->where( array('user_id'=>$user_id) )
            ->order( 'add_time DESC' )
            ->fetchAll();
    }

    // 获取商品头图
    public function getGoodsOriginalImg( $goods_id ) {
        return $this->getORM()
            ->select('original_img,admin_id')
            ->where( array('goods_id'=>$goods_id) )
            ->fetchOne();
    }

    // 删除购物车商品
    public function cartDel( $cart_id ) {
        $orm = \PhalApi\DI()->notorm->cart;
        return $orm
            ->where( array('id'=>$cart_id) )
            ->delete();
    }

    // 获取购物车信息
    public function CartInfo( $con ) {
        $orm = \PhalApi\DI()->notorm->cart;
        return $orm
            ->select('*')
            ->where( $con )
            ->fetchOne();
    }

    // 更新购物车
    public function updateCart( $id, $goods_num ) {
        $orm = \PhalApi\DI()->notorm->cart;
        // 返回影响的行数
        return $orm
            ->where('id', $id)
            ->update(array('goods_num' => $goods_num ));
    }

    // 购物车列表进行添加操作
//    public function updateCartAdd( $user_id, $id ) {
//        $orm = \PhalApi\DI()->notorm->cart;
//        // 返回影响的行数
//        return $orm
//            ->where(array( 'user_id'=>$user_id, 'id' => $id ))
//            ->update(array('goods_num' => new \NotORM_Literal("goods_num + 1")));
//    }
    // 购物车列表进行添加删除操作
    public function updateCartMsg( $user_id, $id, $goods_num ) {
        $orm = \PhalApi\DI()->notorm->cart;
        // 返回影响的行数
        return $orm
            ->where(array( 'user_id'=>$user_id, 'id' => $id ))
            ->update(array('goods_num' => new \NotORM_Literal("goods_num + $goods_num")));
    }
//    // 购物车列表进行添加删除操作
//    public function updateCartDel( $user_id, $id ) {
//        $orm = \PhalApi\DI()->notorm->cart;
//        // 返回影响的行数
//        return $orm
//            ->where(array( 'user_id'=>$user_id, 'id' => $id ))
//            ->update(array('goods_num' => new \NotORM_Literal("goods_num - 1")));
//    }
    // 按条件获取商品
    public function getGoodsProm( $prom_type ) {
        return $this->getORM()
            ->select('goods_id,cat_id,goods_name,original_img,goods_remark,market_price,shop_price,virtual_sales_sum,prom_type,prom_id,prom_price,prom_start_time,prom_end_time,store_count')
            ->where( array( 'prom_type' => $prom_type ) )
            ->fetchAll();
    }

    // 商品类型进行更改
    public function updateGoods( $goods_id ) {
        // 返回影响的行数
        return $this->getORM()
            ->where(array( 'goods_id'=>$goods_id ))
            ->update(array('prom_type' => 0 ));
    }

    // 商品下架
    public function updateOnSale( $goods_id ) {
        // 返回影响的行数
        return $this->getORM()
            ->where(array( 'goods_id'=>$goods_id ))
            ->update(array('is_on_sale' => 0 ));
    }

    // 拼团活动查询
    public function getTeam( $goods_id ) {
        $orm = \PhalApi\DI()->notorm->team_activity;
        // 返回影响的行数
        return $orm
            ->where(array( 'goods_id'=>$goods_id ))
            ->fetchOne();
    }

    // 拼团活动下架
    public function updateTeamStatus( $team_id ) {
        $orm = \PhalApi\DI()->notorm->team_activity;
        // 返回影响的行数
        return $orm
            ->where(array( 'team_id'=>$team_id ))
            ->update(array('status' => 0 ));
    }

    // 删除购物车商品
    public function deleteCartByGoods( $goods_id ) {
        $orm = \PhalApi\DI()->notorm->cart;
        return $orm
            ->where( array('goods_id'=>$goods_id) )
            ->delete();
    }
}
